package com.cenwangkeji.resident.ui.activity.special;


import android.widget.TextView;

import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.OkHttpUtils;
import com.cenwangkeji.resident.model.bean.LaoNianRenBean;
import com.cenwangkeji.resident.model.bean.User;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lws.basic.base.BaseActivity;

import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.RxUtils;
import com.lws.basic.view.adapter.AdapterIntegerDefault0;
import com.lws.basic.view.adapter.DoubleDefault0Adapter;
import com.tencent.bugly.crashreport.CrashReport;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

//老年人中医详情
public class LaoNianRenDetailActivity extends BaseActivity {


    @BindView(R.id.tv_answer_1)
    TextView mTvAnswer1;
    @BindView(R.id.tv_answer_2)
    TextView mTvAnswer2;
    @BindView(R.id.tv_answer_3)
    TextView mTvAnswer3;
    @BindView(R.id.tv_answer_4)
    TextView mTvAnswer4;
    @BindView(R.id.tv_answer_5)
    TextView mTvAnswer5;
    @BindView(R.id.tv_answer_6)
    TextView mTvAnswer6;
    @BindView(R.id.tv_answer_7)
    TextView mTvAnswer7;
    @BindView(R.id.tv_answer_8)
    TextView mTvAnswer8;
    @BindView(R.id.tv_answer_9)
    TextView mTvAnswer9;
    @BindView(R.id.tv_answer_10)
    TextView mTvAnswer10;
    @BindView(R.id.tv_answer_11)
    TextView mTvAnswer11;
    @BindView(R.id.tv_answer_12)
    TextView mTvAnswer12;
    @BindView(R.id.tv_answer_13)
    TextView mTvAnswer13;
    @BindView(R.id.tv_answer_14)
    TextView mTvAnswer14;
    @BindView(R.id.tv_answer_15)
    TextView mTvAnswer15;
    @BindView(R.id.tv_answer_16)
    TextView mTvAnswer16;
    @BindView(R.id.tv_answer_17)
    TextView mTvAnswer17;
    @BindView(R.id.tv_answer_18)
    TextView mTvAnswer18;
    @BindView(R.id.tv_answer_19)
    TextView mTvAnswer19;
    @BindView(R.id.tv_answer_20)
    TextView mTvAnswer20;
    @BindView(R.id.tv_answer_21)
    TextView mTvAnswer21;
    @BindView(R.id.tv_answer_22)
    TextView mTvAnswer22;
    @BindView(R.id.tv_answer_23)
    TextView mTvAnswer23;
    @BindView(R.id.tv_answer_24)
    TextView mTvAnswer24;
    @BindView(R.id.tv_answer_25)
    TextView mTvAnswer25;
    @BindView(R.id.tv_answer_26)
    TextView mTvAnswer26;
    @BindView(R.id.tv_answer_27)
    TextView mTvAnswer27;
    @BindView(R.id.tv_answer_28)
    TextView mTvAnswer28;
    @BindView(R.id.tv_answer_29)
    TextView mTvAnswer29;
    @BindView(R.id.tv_answer_30)
    TextView mTvAnswer30;
    @BindView(R.id.tv_answer_31)
    TextView mTvAnswer31;
    @BindView(R.id.tv_answer_32)
    TextView mTvAnswer32;
    @BindView(R.id.tv_answer_33)
    TextView mTvAnswer33;
    @BindView(R.id.tv_tzbs)
    TextView mTvTzbs;
    @BindView(R.id.tv_zdbj)
    TextView mTvZdbj;
    @BindView(R.id.tv_date)
    TextView mTvDate;

    @Override
    public int setLayoutId() {
        return R.layout.activity_lao_nian_ren_detail;
    }


    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        getData();
    }

    private void getData(){
        showLoading();
        Observable.create(new ObservableOnSubscribe<LaoNianRenBean>() {
            @Override
            public void subscribe(ObservableEmitter<LaoNianRenBean> emitter) throws Exception {
                int id = getIntent().getIntExtra("id", 0);
                Map<Object, Object> map = new HashMap<>();
                map.put("token", User.getToken());
                map.put("mark", 3);
                map.put("zxbId", id);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                Gson gson = new GsonBuilder()
                        .registerTypeAdapter(Integer.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(int.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(Double.class, new DoubleDefault0Adapter())
                        .registerTypeAdapter(double.class, new DoubleDefault0Adapter())
                        .create();
                String json = OkHttpUtils.post(jsonObject.toString(),Constant.lao_nian_ren_detail);
                MyLogger.i(json);
                emitter.onNext(gson.fromJson(json, LaoNianRenBean.class));


            }
        }).compose(RxUtils.<LaoNianRenBean>io_main()).subscribe(new ObserverOnNext<LaoNianRenBean>() {
            @Override
            public void onNext(LaoNianRenBean bean) {
                try {
                    dismissLoading();
                    if (bean.getData() != null) {
                        mTvAnswer1.setText(answer(bean.getData().getLnrWt1()));
                        mTvAnswer2.setText(answer(bean.getData().getLnrWt2()));
                        mTvAnswer3.setText(answer(bean.getData().getLnrWt3()));
                        mTvAnswer4.setText(answer(bean.getData().getLnrWt4()));
                        mTvAnswer5.setText(answer(bean.getData().getLnrWt5()));
                        mTvAnswer6.setText(answer(bean.getData().getLnrWt6()));
                        mTvAnswer7.setText(answer(bean.getData().getLnrWt7()));
                        mTvAnswer8.setText(answer(bean.getData().getLnrWt8()));
                        mTvAnswer9.setText(answer(bean.getData().getLnrWt9()));
                        mTvAnswer10.setText(answer(bean.getData().getLnrWt10()));
                        mTvAnswer11.setText(answer(bean.getData().getLnrWt11()));
                        mTvAnswer12.setText(answer(bean.getData().getLnrWt12()));
                        mTvAnswer13.setText(answer(bean.getData().getLnrWt13()));
                        mTvAnswer14.setText(answer(bean.getData().getLnrWt14()));
                        mTvAnswer15.setText(answer(bean.getData().getLnrWt15()));
                        mTvAnswer16.setText(answer(bean.getData().getLnrWt16()));
                        mTvAnswer17.setText(answer(bean.getData().getLnrWt17()));
                        mTvAnswer18.setText(answer(bean.getData().getLnrWt18()));
                        mTvAnswer19.setText(answer(bean.getData().getLnrWt19()));
                        mTvAnswer20.setText(answer(bean.getData().getLnrWt20()));
                        mTvAnswer21.setText(answer(bean.getData().getLnrWt21()));
                        mTvAnswer22.setText(answer(bean.getData().getLnrWt22()));
                        mTvAnswer23.setText(answer(bean.getData().getLnrWt23()));
                        mTvAnswer24.setText(answer(bean.getData().getLnrWt24()));
                        mTvAnswer25.setText(answer(bean.getData().getLnrWt25()));
                        mTvAnswer26.setText(answer(bean.getData().getLnrWt26()));
                        mTvAnswer27.setText(answer(bean.getData().getLnrWt27()));
                        mTvAnswer28.setText(answer(bean.getData().getLnrWt28()));
                        mTvAnswer29.setText(answer(bean.getData().getLnrWt29()));
                        mTvAnswer30.setText(answer(bean.getData().getLnrWt30()));
                        mTvAnswer31.setText(answer(bean.getData().getLnrWt31()));
                        mTvAnswer32.setText(answer(bean.getData().getLnrWt32()));
                        mTvAnswer33.setText(answer(bean.getData().getLnrWt33()));
                        mTvTzbs.setText(zybs(bean.getData().getLnrTzbs(), bean.getData().getLnrTzlx()));
                        mTvZdbj.setText(bjzd(bean.getData().getLnrZyybjzdList(), bean.getData().getLnrZyybjzdQt()));
                        mTvDate.setText("时间：" + bean.getData().getCreateDate());
                    } else {
                        MyToast.show(bean.getMsg());
                    }
                }catch (Exception e){
                    MyToast.show(e.getMessage());
                    CrashReport.postCatchedException(e);
                }
            }
        });

    }

    //根据索引返回对应的字符串
    private String answer(int index){
        if (1==index)
            return "没有(根本不/从 来没有)";
        if (2==index)
            return "很少(有一点/偶尔)";
        if (3==index)
            return "有时(有些/少数时间)";
        if (4==index)
            return "经常(相当/多数时间)";
        if (5==index)
            return "总是(非常/每天)";
        return "未填";
    }
    //体质
    private String zybs(int bs,int lx){
        String zybs = "";
        switch (bs){
            case 1:
                zybs ="气虚质";
                break;
            case 2:
                zybs ="阳虚质";
                break;
            case 3:
                zybs ="阴虚质";
                break;
            case 4:
                zybs ="痰湿质";
                break;
            case 5:
                zybs ="湿热质";
                break;
            case 6:
                zybs ="血瘀质";
                break;
            case 7:
                zybs ="气郁质";
                break;
        }
        if (lx==1)
            return zybs+"  基本是";
        if (lx==2)
            return zybs+"  是";
        if (lx==3)
            return zybs+"  倾向是";
    return  zybs;

    }

    //保健指导
    private String bjzd(List<String> list,String qt){
        String bjzd ="";
        for (int i = 0; i<list.size(); i++){
            switch (list.get(i)){
                case "1":
                    bjzd += "情志调摄 ";
                    break;
                case "2":
                    bjzd += "饮食调养 ";
                    break;
                case "3":
                    bjzd += "起居调摄 ";
                    break;
                case "4":
                    bjzd += "链霉素 ";
                    break;
                case "5":
                    bjzd += "穴位保健 ";
                    break;
                case "6":
                    bjzd += qt;
                    break;

            }
        }
        return bjzd;

    }
}
